package util

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"time"
)

func Back(ctx *gin.Context, isSuccess bool, message string, data gin.H) {
	if isSuccess {
		ctx.JSON(200, gin.H{
			"message": message,
			"code":    200,
			"data":    data,
		})
	} else {
		ctx.JSON(200, gin.H{
			"message": message,
			"code":    201,
			"data":    data,
		})
	}
}

func GenerateToken(secret string, mail string, password string, nickname string) string {
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
		"timestamp": time.Now().Unix(),
		"mail":      mail,
		"password":  password,
		"nickname":  nickname,
	})
	tokenString, _ := token.SignedString([]byte(secret))
	return tokenString
}
